﻿using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace AP_GestArm
{
    class BaseDados
    {

        #region Declarações

        public DataSet d7 = new DataSet("BD");
        Boolean Check;        
        public OleDbDataAdapter da;
        OleDbCommandBuilder Builder;
        OleDbConnection Conn;

        #endregion

        public BaseDados(string bdPath)
        {
            if (!File.Exists(bdPath))
            {
                #region BaseDados Access

                string Destino = Application.StartupPath + "\\Data\\bd.mdb";
                OpenFileDialog Abrircomo = Openfile.OpenFile("Base de dados não existe. Escolha o ficheiro por favor.",
                                                    "Base de Dados Access (*.mdb)|*.mdb");

                if (Abrircomo.ShowDialog() == DialogResult.OK)
                {
                    string Path = Abrircomo.FileName;
                    File.Copy(Path, Destino, false);
                }

                #endregion
            }

            Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + bdPath);
            this.d7.Tables.Add("Bitolas");
            this.d7.Tables.Add("Lugares");

            Check = true;

            if (this.Check)
            {
                da = new OleDbDataAdapter("select * from Bitolas", Conn);
                Builder = new OleDbCommandBuilder(da);
                da.InsertCommand = Builder.GetInsertCommand();
                da.DeleteCommand = Builder.GetDeleteCommand();
                da.UpdateCommand = Builder.GetUpdateCommand();
                da.Fill(this.d7.Tables["Bitolas"]);
            }
        }

        public void Locais()
        {
            if (this.Check)
            {
                da = new OleDbDataAdapter("select * from Lugares", Conn);
                Builder = new OleDbCommandBuilder(da);
                da.InsertCommand = Builder.GetInsertCommand();
                da.DeleteCommand = Builder.GetDeleteCommand();
                da.UpdateCommand = Builder.GetUpdateCommand();
                da.Fill(this.d7.Tables["Lugares"]);
            }
        }
    }
}
